package com.ruoyi.common.resp.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ruoyi.common.resp.enums.ErrorCodeConstants;
import com.ruoyi.common.resp.exception.ErrorCode;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Objects;

/**
 * 通用返回
 *
 * @param <T> 数据泛型
 */
@Data
public class R<T> implements Serializable {

    /**
     * 错误码
     *
     * @see ErrorCode#getCode()
     */
    private int code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误提示，用户可阅读
     *
     * @see ErrorCode#getMsg() ()
     */
    private String msg;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 500;

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(data, SUCCESS, msg);
    }


    public static <T> R<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> R<T> fail(String msg, T data) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        Assert.isTrue(!isSuccess(code), "code 必须是错误的！");
        return restResult(null, code, msg);
    }

    public static <T> R<T> fail(int code, String msg, T data) {
        Assert.isTrue(!isSuccess(code), "code 必须是错误的！");
        return restResult(data, code, msg);
    }

    public static <T> R<T> success() {
        return restResult(null, ErrorCodeConstants.SUCCESS.getCode(), "操作成功");
    }

    public static <T> R<T> success(T data) {
        return restResult(data, ErrorCodeConstants.SUCCESS.getCode(), "操作成功");
    }

    public static <T> R<T> success(String msg) {
        return restResult(null, ErrorCodeConstants.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> success(String msg, T data) {
        return restResult(data, ErrorCodeConstants.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> error() {
        return restResult(null, ErrorCodeConstants.FAIL.getCode(), "操作失败");
    }

    public static <T> R<T> error(String msg) {
        return restResult(null, ErrorCodeConstants.FAIL.getCode(), msg);
    }

    public static <T> R<T> error(T data) {
        return restResult(data, ErrorCodeConstants.FAIL.getCode(), "操作失败");
    }

    public static <T> R<T> error(String msg, T data) {
        return restResult(data, ErrorCodeConstants.FAIL.getCode(), msg);
    }

    public static <T> R<T> error(int code, String msg) {
        Assert.isTrue(!isSuccess(code), "code 必须是错误的！");
        return restResult(null, code, msg);
    }

    public static <T> R<T> error(int code, String msg, T data) {
        Assert.isTrue(!isSuccess(code), "code 必须是错误的！");
        return restResult(data, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static boolean isSuccess(int code) {
        return Objects.equals(code, ErrorCodeConstants.SUCCESS.getCode()) || Objects.equals(code, SUCCESS);
    }

    @JsonIgnore // 避免 jackson 序列化
    public boolean isSuccess() {
        return isSuccess(code);
    }

    @JsonIgnore // 避免 jackson 序列化
    public boolean isError() {
        return !isSuccess();
    }

}
